﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using WpfHostedXna;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Crystal.Mapping;
using System.Collections.ObjectModel;
using Crystal.libs;
using System.Diagnostics;
using System.Reflection;

namespace LevelEditor.ViewModel
{
    public class LevelEditorViewModel : INotifyPropertyChanged
    {
        //Fields 
        #region Cells
        private ObservableCollection<CellIcon> _AvailableCells;
        public ObservableCollection<CellIcon> AvailableCells
        {
            get { return _AvailableCells; }
            set { _AvailableCells = value; }
        }

        private CellIcon _SelectedCellType;
        public CellIcon SelectedCellType
        {
            get { return _SelectedCellType; }
            set { _SelectedCellType = value; GetPropertiesFromCell(); RaisePropertyChanged("SelectedCellType"); }
        } 

        #endregion

        #region Toolkit
            
        private ObservableCollection<ToolkitItem> _Toolkit; 
		public ObservableCollection<ToolkitItem> Toolkit
		{
			get { return _Toolkit; }
			set { _Toolkit = value; }
		}

        private ToolkitItem _SelectedToolkit;
        public ToolkitItem SelectedToolkit
		{
            get { return _SelectedToolkit; }
            set { _SelectedToolkit = value; RaisePropertyChanged("SelectedToolkit"); }
		}

        #endregion
        
        //Commands

        //ctor
        public LevelEditorViewModel()
        {
            CreateCommands();
            ReflectCells();
            SetupToolkit();
        }

        //Methods
        #region View Calls
        public void Render(GraphicsDeviceEventArgs e)
        {
            DepthStencilState ds = new DepthStencilState();
            ds.DepthBufferEnable = true;
            RasterizerState rs = new RasterizerState();
            rs.CullMode = CullMode.None;
            rs.FillMode = FillMode.Solid;

            e.GraphicsDevice.RasterizerState = rs;
            e.GraphicsDevice.DepthStencilState = ds;
            e.GraphicsDevice.Clear(Color.DarkSlateBlue);

            //Render ...
        }
        public void Load(GraphicsDeviceEventArgs e)
        {
            //Load content?
        }
        public void MouseDown(HwndMouseEventArgs e)
        {

        }
        public void MouseUp(HwndMouseEventArgs e)
        {

        }
        public void DoubleClick(HwndMouseEventArgs e)
        {

        }
        #endregion
        
        private void CreateCommands()
        {

        }
                
        private void ReflectCells()
        {
            //IEnumerable<Type> cells = GetTypesWith<CellAttribute>();
            //ObservableCollection<CellIcon> temp = new ObservableCollection<CellIcon>();

            //int index = 0;
            //foreach (Type cell in cells)
            //{
            //    PropertyInfo[] properties = cell.GetProperties();
            //    foreach(PropertyInfo propInfo in properties)
            //    {
            //        object[] attribs = propInfo.GetCustomAttributes(true);

            //        foreach (object o in attribs)
            //        {
            //            Debug.WriteLine(propInfo.Name + " Has :" + o.GetType());
            //        }
            //    }


            //    CellIcon i = new CellIcon()
            //    {
            //        Name = cell.Name,
            //        Index = index,
            //    };
            //    temp.Add(i);
            //    index++;
            //}

            //AvailableCells = temp;
        }
        private void SetupToolkit()
        {
            ObservableCollection<ToolkitItem> items = new ObservableCollection<ToolkitItem>();

            items.Add(new ToolkitItem() { TKOptionName = "Cells" });
            items.Add(new ToolkitItem() { TKOptionName = "Walkers" });
            items.Add(new ToolkitItem() { TKOptionName = "AI" });
            items.Add(new ToolkitItem() { TKOptionName = "Exit" });

            Toolkit = items;
        }
        private void GetPropertiesFromCell()
        {

        }
        
        IEnumerable<Type> GetTypesWith<TAttribute>(bool inherit = true)
            where TAttribute : System.Attribute
        {
            return from a in AppDomain.CurrentDomain.GetAssemblies()
                   from t in a.GetTypes()
                   where t.IsDefined(typeof(TAttribute), inherit)
                   select t;
        }
        
        #region INotifyProperty
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string param)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(param));

        }
        #endregion
    }

    
}